package com.zjitc.controller;

import com.zjitc.common.ServerResponse;
import com.zjitc.model.Comment;
import com.zjitc.model.Pay;
import com.zjitc.model.Product;
import com.zjitc.model.ProductVo;
import com.zjitc.model.User;
import com.zjitc.service.ProductService;
import java.util.List;
import javax.print.DocFlavor.STRING;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/5/2 16:50
 * @description:
 */
@Controller
@RequestMapping("/product")
public class ProductController {

  @Autowired
  private ProductService productService;

  @RequestMapping("/addProduct")
  public String addProduct() {
    return "picture";
  }

  @RequestMapping("/productAll")
  public String productAll(HttpSession session) {
    List<Product> list = productService.productAll();
    session.setAttribute("product", list);
    return "product";
  }

  @RequestMapping("/pushProduct")
  public String pushProduct(Product product, HttpSession session) {

    ServerResponse<Product> response = productService.pushProduct(product);
    if (response.getStatus() == 1) {
      session.setAttribute("pushProductMSG", "该商品添加失败");
      return "picture";
    } else {
      session.removeAttribute("registerMSG");
      return "index";
    }
  }

  @RequestMapping("/showProduct")
  public String showProduct(Integer id, HttpSession session) {
    Product product = productService.showProduct(id);
    session.setAttribute("showProduct", product);
    return "showProduct";
  }

  @RequestMapping("/delete")
  public String delete(Integer id, HttpSession session) {
    List<Product> list = productService.productAll();
    session.setAttribute("product", list);
    productService.delete(id);
    return "product";
  }

  @RequestMapping("/showComment")
  public String showComment(HttpServletRequest request) {
    List<ProductVo> products = productService.showComment();
    for (ProductVo p : products) {
      System.out.println(p.toString());
    }
    request.setAttribute("products", products);
    return "rookiecircle";
  }

  @RequestMapping("/comment")
  public String comment(
      @RequestParam(value = "com", required = false) String com,
      @RequestParam("pid") Integer pid,
      HttpSession session) {
    User user = (User) session.getAttribute("user");
    productService.comment(com, pid, user.getId());
    return "forward:/product/showComment.do";
  }



  @RequestMapping("/record")
  public String record(HttpSession session,String money,String username, String name) {
    session.setAttribute("money", money);
    String uid = productService.creatUid();
    session.setAttribute("uuid", uid);
   productService.payAdd(money,username, name);
    return "aliyun/alipay.trade.page.pay";
  }
}

